<template>
  <div class="department-container">
    <el-card>
      <!-- 用一个行列布局 -->
      <el-row
        type="flex"
        justify="space-between"
        align="middle"
        style="height: 40px"
      >
        <el-col :span="20">
          <svg-icon icon-class="home" /><span>传智教育</span>
        </el-col>
        <el-col :span="4">
          <el-row type="flex" justify="end">
            <!-- 两个内容 -->
            <el-col>负责人</el-col>
            <!-- 下拉菜单 element -->
            <el-col>
              <el-dropdown>
                <span> 操作<i class="el-icon-arrow-down" /> </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="addDep('')">添加子部门</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
      <el-tree :data="list" :default-expand-all="true">
        <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
        <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
        <template #default="{ data }">
          <el-row
            type="flex"
            justify="space-between"
            align="middle"
            style="height: 40px; width: 100%"
          >
            <el-col :span="20">
              <span>{{ data.name }}</span>
            </el-col>
            <el-col :span="4">
              <el-row type="flex" justify="end">
                <!-- 两个内容 -->
                <el-col>{{ data.manager }}</el-col>
                <el-col>
                  <!-- 下拉菜单 element -->
                  <el-dropdown>
                    <span> 操作<i class="el-icon-arrow-down" /> </span>
                    <!-- 下拉菜单 -->
                    <el-dropdown-menu slot="dropdown">
                      <el-dropdown-item @click.native="addDep(data.id)">添加子部门</el-dropdown-item>
                      <el-dropdown-item @click.native="editDep(data)">编辑部门</el-dropdown-item>
                      <el-dropdown-item @click.native="delDep(data.id)">删除部门</el-dropdown-item>
                    </el-dropdown-menu>
                  </el-dropdown>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </el-card>
    <!-- 添加部门 -->
    <el-dialog :title="isEdit ? '编辑部门' : '添加部门'" :visible.sync="dialogFormVisible">
      <depAddDia
        v-if="dialogFormVisible"
        ref="deptForm"
        :dialog-form-visible.sync="dialogFormVisible"
        :pid="pid"
        :is-edit="isEdit"
        :row="row"
        @success="success"
      />
    </el-dialog>
  </div>
</template>

<script>
var arrayToTree = require('array-to-tree')
import { getDepartments, delDepartment } from '@/api/departments'
// import { tranListToTreeData } from '@/utils'
import depAddDia from './deptDialog.vue'

export default {
  components: {
    depAddDia
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      dialogFormVisible: false, // 是否显示弹层
      pid: '',
      isEdit: false,
      row: {}
    }
  },
  created() {
    this.loadDep()
  },
  methods: {
    async loadDep() {
      try {
        const res = await getDepartments()
        // console.log(res)
        // 由于业务中传智教育的pid是:-1，它是最高级的节点
        // shift 从数组头部取出一个元素
        // eslint-disable-next-line no-unused-vars
        const root = res.data.depts.shift()
        // console.log(root)
        // 用工具函数把 平铺的数组转成 树型数据
        // this.list = tranListToTreeData(res.data.depts)
        this.list = arrayToTree(res.data.depts, {
          parentProperty: 'pid',
          customID: 'id'
        })
      } catch (err) {
        // console.log(err)
      }
    },
    // 添加部门
    addDep(pid) {
      // console.log('111')
      this.dialogFormVisible = true
      this.pid = pid
      this.isEdit = false // 点击的是添加按钮
    },
    success() {
      // 添加部门成功后执行
      this.dialogFormVisible = false
      this.$refs.deptForm.reset()
      this.loadDep()
    },
    // 删除部门
    async delDep(id) {
      const res = await this.$confirm(
        '确认删除?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      if (res === 'confirm') {
        // 表示点击的是确认按钮
        // alert(id)
        //  4-3 调用api接口函数
        const result = await delDepartment(id)
        // console.log(result, 89)
        if (result.code === 10000) {
          // 4-4 成功后=====刷新页面
          this.loadDep()
        }
      }
    },
    // 编辑部门
    editDep(row) {
      this.isEdit = true // 点击的是编辑按钮
      // 显示对话框
      this.dialogFormVisible = true
      // 获取当前行的数据
      this.row = row
    }
  }
}
</script>
